package com.caoustc.okhttplib.okhttp

import java.util.*
import java.util.concurrent.ConcurrentHashMap

class HttpTaskHandler private constructor() {
    private val mHttpTaskMap: MutableMap<String?, MutableSet<String?>>
    fun removeTask(key: String?) {
        if (mHttpTaskMap.containsKey(key)) {
            //移除对应的Key
            mHttpTaskMap.remove(key)
        }
    }

    fun addTask(key: String?, task: String?) {
        if (mHttpTaskMap.containsKey(key)) {
            val tasks = mHttpTaskMap[key]!!
            tasks.add(task)
            mHttpTaskMap[key] = tasks
        } else {
            val tasks: MutableSet<String?> = HashSet()
            tasks.add(task)
            mHttpTaskMap[key] = tasks
        }
    }

    operator fun contains(key: String?): Boolean {
        return mHttpTaskMap.containsKey(key)
    }

    companion object {
        @Volatile
        private var mHttpTaskHandler: HttpTaskHandler? = null
        @kotlin.jvm.JvmStatic
        val instance: HttpTaskHandler?
            get() {
                synchronized(HttpTaskHandler::class.java) {
                    if (mHttpTaskHandler == null) {
                        mHttpTaskHandler = HttpTaskHandler()
                    }
                }
                return mHttpTaskHandler
            }
    }

    init {
        mHttpTaskMap = ConcurrentHashMap()
    }
}